import { EventTarget } from "cc";

export enum GlobalEventsType {
    socketClose = "socketClose",//服务器断开连接会带参数closeCode
    socketOpen = "socketOpen",//主服务器连接并验证成功
    chatsSocketClose = "chatsSocketClose",//聊天服务器断开连接 会带参数closeCode
    chatSocketOpen = "chatSocketOpen",//聊天服务器连接并验证成功
    dialogViewOpen = "dialogViewOpen",//窗口打开成功 会把DialogViewID传过去
    dialogViewClose = "dialogViewClose",//窗口关闭  会把DialogViewID传过去
}

/**全局事件*/
export class GlobalEventsMgr {
    private static _instance:GlobalEventsMgr;
    private _eventTarget:EventTarget = new EventTarget();
    public static get instance():GlobalEventsMgr{
        if(this._instance==null){
            this._instance = new GlobalEventsMgr();
        }
        return this._instance
    }
    public emit(type:GlobalEventsType,data?):void{
        this._eventTarget.emit(type,data);
    }
    /**监听全局事件*/
    public on(type:GlobalEventsType, callback:any,thisArg?: any, once?: boolean):void{
        this._eventTarget.on(type,callback,thisArg,once);
    }
    /**清理全局事件*/
    public off(type:GlobalEventsType,callback:any,thisArg?: any,):void{
        this._eventTarget.off(type,callback,thisArg);
    }
    public offAll(thisArg: any):void{
        this._eventTarget.removeAll(thisArg);
    }
}